#include "Controller.h"

//extern std::list<Kpoint* > POLY_POINTS;
/*
void test()
{
	//CIMG
	Image image(10, 10, 1, channelsNumber, 0);
	Image input("facebook_znaczek.jpg");
	cimg_library::CImgDisplay disp(image,"result");




	//SFML
	// Create a new render-texture
	sf::RenderWindow window(sf::VideoMode(10, 10), "SFML window");
	sf::RenderTexture texture;
	sf::Image originalImage;
	originalImage.loadFromFile("facebook_znaczek.jpg");

	texture.create(10, 10);



	ImageComparator comparator;
	Polygons* triangles = new Polygons();
	Polygons* trianglesBackup = new Polygons();
	TriangleBuilder builder;
	Mutator mutator(builder, &comparator);


	Verticles* _verticles = new Verticles(3, 2, 1, 1, 0);  // TO DO   wywalic 3

	(*_verticles)(0, 0) = 0;
	(*_verticles)(0, 1) = 0;

	(*_verticles)(1, 0) = 9;
	(*_verticles)(1, 1) = 0;

	(*_verticles)(2, 0) = 5;
	(*_verticles)(2, 1) = 5;
	
	ColorT* _color = new ColorT[3];  //TO DO jw.

	(_color)[0]= 255;
	(_color)[1]= 100;
	(_color)[2]= 10;


	OpacityT _opacity = 0.15;

	CPolygon* triangle = new CPolygon(_verticles, _color, _opacity);


	Verticles* _verticles2 = new Verticles(3, 2, 1, 1, 0);  // TO DO   wywalic 3

	(*_verticles2)(0, 0) = 5;
	(*_verticles2)(0, 1) = 0;

	(*_verticles2)(1, 0) = 9;
	(*_verticles2)(1, 1) = 0;

	(*_verticles2)(2, 0) = 9;
	(*_verticles2)(2, 1) = 9;

	ColorT* _color2 = new ColorT[3];  //TO DO jw.

	(_color2)[0]= 10;
	(_color2)[1]= 100;
	(_color2)[2]= 255;


	OpacityT _opacity2 = 0.24;

	CPolygon* triangle2 = new CPolygon(_verticles2, _color2, _opacity2);
	
	texture.clear();
	image.fill(0);

	triangle->draw(image);
//	triangle->drawSFML(texture);

	triangle2->draw(image);
	//triangle2->drawSFML(texture);

	//triangles->push_back(triangle2);



	disp.display(image);
	texture.display();

	// Draw the texture
	sf::Sprite sprite(texture.getTexture());
	window.draw(sprite);
	// End the current frame and display its contents on screen
	window.display();

	disp.display(image);

	image.save("CIMG.bmp");
	sf::Image& generatedImage = texture.getTexture().copyToImage();
	generatedImage.saveToFile("SFML.bmp");
	
	std::ofstream cimg("cimg.txt");
	std::ofstream sfml("sfml.txt");
	for(int j = 0; j<10; j++) //wysokosc
	{
		for(int i=0; i<10; i++) //szzerokosc
		{
			cimg<<i<<", "<<j<<": "<<(int)image(i,j,0,0)<<" "<<(int)image(i,j,0,1)<<" "<<(int)image(i,j,0,2)<<std::endl;
			sfml<<i<<", "<<j<<": "<<(int)generatedImage.getPixel(i, j).r<<" "<<(int)generatedImage.getPixel(i, j).g<<" "<<(int)generatedImage.getPixel(i, j).b<<std::endl;
		}
	}
	cimg.close();
	sfml.close();
}

void pixelTest()
{
	Image image(200, 200, 1, channelsNumber, 0);
	Verticles* _verticles2 = new Verticles(3, 2, 1, 1, 0);  // TO DO   wywalic 3

	(*_verticles2)(0, 0) = 10;
	(*_verticles2)(0, 1) = 10;

	(*_verticles2)(1, 0) = 125;
	(*_verticles2)(1, 1) = 150;

	(*_verticles2)(2, 0) = 33;
	(*_verticles2)(2, 1) = 111;

	ColorT* _color2 = new ColorT[3];  //TO DO jw.

	(_color2)[0]= 100;
	(_color2)[1]= 100;
	(_color2)[2]= 255;

	ColorT* _color3 = new ColorT[3];  //TO DO jw.

	(_color3)[0]= 200;
	(_color3)[1]= 000;
	(_color3)[2]= 5;

	image.draw_polygon(*_verticles2, _color2, 0.31);

	image.draw_rectangle(10,10, 125,150, _color3, 0.3);
	image.save("OUT.jpg");
}

*/
	/*
extern std::set<Kpoint , KpointComp> polyPoints ;


void patchTest()
{
	cimg_library::CImg<int> image(200, 200, 1, channelsNumber, 0);
	cimg_library::CImg<int> verticles(5,2, 1, 1, 0);
	(verticles)(0,0) = 1;
	(verticles)(0,1) = 1;

	(verticles)(1,0) = 200; 
	(verticles)(1,1) = 1;

	(verticles)(2,0) = 100;    //Two points
	(verticles)(2,1) = 150;    //with the same coordinates

	(verticles)(3,0) = 100;    //one after
	(verticles)(3,1) = 150;    //another

	(verticles)(4,0) = 90;
	(verticles)(4,1) = 70;


	int color[3] = {255,100,200};
	clock_t tt;
	tt = clock();

	for(int i=0; i<10000; i++)
	{
		image.fill(0);
		image.draw_polygon(verticles, color, 0.6);  //must be transparent

	}

	tt = clock() - tt;
	//// std::cout<< "seconds of drawing one polygon: "<<(((float)tt)/CLOCKS_PER_SEC)<<std::endl;

	//image.display();
	//image.save("before4sec06.bmp");

	system("PAUSE");


}

bool fncomp (Kpoint lhs, Kpoint rhs) {
	if(lhs.y < rhs.y)
	{
		return true;
	}
	else
		if(lhs.y == rhs.y)
		{
			return lhs.x < rhs.x;

		}else
			return false;
}


void cimgModifications()
{
	Image image(200, 200, 1, channelsNumber, 0);




	srand(26);
	for(int i=0; i<1000000; i++)
	{
		image.fill(0);
		polyPoints.clear();


		int verticlesNumber = random(3, 4);
		cimg_library::CImg<int> verticles(verticlesNumber,2, 1, 1, 0);

		for(int i=0; i<verticles.width(); i++)
		{
			(verticles)(i,0) = random(0,200);
			(verticles)(i,1) = random(0,200);
		}

		if(i == 3)
		{
			//// std::cout<<"3"<<std::endl;
		}
		image.draw_polygon(verticles, color, randomToOne());  //must be transparent

		std::set<Kpoint , KpointComp> polyPointsCheck;
		for(int j=0; j<200; j++)
			for(int ii = 0; ii <200; ii++)
			{
				if((int)image(ii, j, 0)!=0)
				{
					Kpoint kkk;
					kkk.x = ii;
					kkk.y = j;
					polyPointsCheck.insert(kkk);
					////// std::cout<<i<<","<<j<<": "<<(int)image(i, j, 0)<<" "<<std::endl;
				}
			}


			std::set<Kpoint , KpointComp>::iterator it = polyPointsCheck.begin(); 
			std::set<Kpoint , KpointComp>::iterator it2 = polyPoints.begin(); 
			for(;it != polyPointsCheck.end(); it++, it2++)
			{
				if((it->x != it2->x) || (it->y != it2->y))
				{
					//// std::cout<<"FAIL"<<std::endl;
				}


			}

		
	}
	//tt = clock() - tt;
	////// std::cout<< "seconds of drawing one polygon: "<<(((float)tt)/CLOCKS_PER_SEC)<<std::endl;

//	image.display();
//	image.save("before4sec06.bmp");

		//// std::cout<<std::endl;
		for(int j=0; j<20; j++)
		for(int i = 0; i <20; i++)
		{
			if((int)image(i, j, 0)!=0)
			{

			
			//// std::cout<<i<<","<<j<<": "<<(int)image(i, j, 0)<<" "<<std::endl;
			}
		}

	system("PAUSE");


}
*/
int main(int, char**)
{
	//cimgModifications();
	//// std::cout<<sizeof(void*);

	Controller controler("ml.jpg", "ml", /*6500*/100000);
	return 0;
}